package cool.taomu.toolkit.apilog

import com.google.common.eventbus.Subscribe
import com.google.inject.Inject
import com.google.inject.name.Named
import cool.taomu.toolkit.apilog.entity.ApiCallEventEntity
import cool.taomu.toolkit.dbutils.DbUtils

class ApiLogHandler {

    @Inject
    DbUtils dbutils;

    @Inject(optional=true)
    @Named("taomu.apilog.name")
    String name = "localhost";

    @Subscribe
    def onEvent(ApiCallEventEntity event) {
        println(event)
        val sqlStr = new StringBuffer("insert into")
        sqlStr.append(" taomu_api_call_log(name,method_name,http_method,path) ")
        sqlStr.append(" value(?,?,?,?)")
        dbutils.transaction([
            it.update(sqlStr.toString, name, event.methodName, event.httpMethod, event.path)
            null
        ])
    }
}
